﻿using System;
using System.Collections.Generic;
using System.Linq;
using IInject.Internal;

namespace IInject.Registration.Scanning
{
	/// <summary>
	/// 
	/// </summary>
	public class ScanningContext : ContainerMember
	{
		private readonly int _conventionCount;
		private readonly IScanningRule[] _conventions;

		/// <summary>
		/// Initializes a new instance of the <see cref="ScanningContext"/> class.
		/// </summary>
		/// <param name="container">The container.</param>
		/// <param name="conventions">The conventions.</param>
		public ScanningContext(IContainer container, IScanningRule[] conventions) : base(container)
		{
			_conventions = conventions;
			_conventionCount = conventions.Length;
			PendingItems = new List<MatchingResult>();
		}

		/// <summary>
		/// Gets the pending items.
		/// </summary>
		/// <value>
		/// The pending items.
		/// </value>
		protected List<MatchingResult> PendingItems { get; private set; }

		/// <summary>
		/// Adds the type mapping to the pending items if it matched.
		/// </summary>
		/// <param name="type">The type.</param>
		public void AddIfMatched(Type type)
		{
			for (int i = 0; i < _conventionCount; i++)
			{
				var result = _conventions[i].IsMatch(type);

				if (result.IsMatch)
				{
					PendingItems.Add(result);
					break;
				}
			}
		}

		/// <summary>
		/// Gets the pending items.
		/// </summary>
		/// <returns></returns>
		public MatchingResult[] GetPendingItems()
		{
			return PendingItems.ToArray();
		}
	}
}
